import 'package:Second/routerdemo/AppBarDemoPage.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

//自定义组件
class MyApp extends StatelessWidget {
  final Map<String, Function> routes = {
    "/": (context) => TabsLogin(),
    "/appBarDemo": (context) => AppBarDemoPage()
  };
  @override
  Widget build(BuildContext context) {
    // MaterialApp是根组件
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      // home: TabsLogin(),
      initialRoute: '/',
      onGenerateRoute: (RouteSettings settings) {
        final String? name = settings.name;
        final Function? pageContentBuilder = this.routes[name];

        if (pageContentBuilder != null) {
          if (settings.arguments != null) {
            final Route route = MaterialPageRoute(
                builder: (context) =>
                    pageContentBuilder(context, arguments: settings.arguments));
            return route;
          } else {
            final Route route = MaterialPageRoute(
                builder: (context) => pageContentBuilder(context));
            return route;
          }
        }
      },
      theme: ThemeData(primaryColor: Colors.blue),
    );
  }
}

class TabsLogin extends StatefulWidget {
  final int index;
  TabsLogin({Key? key, this.index = 0}) : super(key: key);

  @override
  _TabsState createState() => _TabsState(index);
}

class _TabsState extends State<TabsLogin> {
  int _currentIndex = 0;
  List pageList = [HomePage(), HomePage(), HomePage(), HomePage(), HomePage()];

  _TabsState(index) {
    this._currentIndex = index;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('FloatingActionButton')),
      floatingActionButton: FloatingActionButton(
        // shape: RoundedRectangleBorder(
        //     borderRadius: BorderRadius.all(Radius.circular(5))),
        backgroundColor: Colors.amber,
        child: Icon(
          Icons.add,
          color: Colors.white,
          size: 20,
        ),
        onPressed: () {
          setState(() {
            this._currentIndex = 1;
          });
        },
      ),
      // floatingActionButton: Container(
      //   height: 60,
      //   width: 60,
      //   decoration: BoxDecoration(
      //       borderRadius: BorderRadius.circular(200), color: Colors.red),
      //   child: FloatingActionButton(
      //     backgroundColor: Colors.amber,
      //     child: Icon(
      //       Icons.add,
      //       color: Colors.white,
      //       size: 20,
      //     ),
      //     onPressed: () {
      //       setState(() {
      //         this._currentIndex = 1;
      //       });
      //     },
      //   ),
      // ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      body: pageList[_currentIndex],
      //第一种样式
      // bottomNavigationBar: BottomNavigationBar(
      //   type: BottomNavigationBarType.fixed,
      //   items: [
      //     BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页1"),
      //     BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页2"),
      //     BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页3"),
      //     BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页4"),
      //     BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页5"),
      //   ],
      //   currentIndex: this._currentIndex,
      //   //bottomNavigationBar的点击事件响应
      //   onTap: (index) {
      //     setState(() {
      //       this._currentIndex = index;
      //     });
      //   },
      // ),
      //=======================================================
      //第二种样式
      bottomNavigationBar: BottomAppBar(
        color: Theme.of(context).primaryColor,
        shape: CircularNotchedRectangle(),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            IconButton(onPressed: () {}, icon: Icon(Icons.add)),
            SizedBox(),
            IconButton(onPressed: () {}, icon: Icon(Icons.home))
          ],
        ),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      //tabbar的个数
      length: 2,
      child: Scaffold(
        appBar: AppBar(
          //TabBar给title和给bottom界面显示位置不一样
          title: TabBar(
            tabs: [
              Tab(
                text: "热销",
              ),
              Tab(text: "热销")
            ],
          ),
        ),
        body: TabBarView(
          children: [
            Column(children: [
              ElevatedButton(
                  onPressed: () {
                    Navigator.pushNamed(context, "/appBarDemo");
                  },
                  child: Text('appBarDemo')),
            ]),
            ListView(children: [
              ListTile(
                title: Text("销量数据"),
              ),
              ListTile(title: Text("销量数据2"))
            ])
          ],
        ),
      ),
    );
  }
}
